package com.dartfar.permission.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.dartfar.base.action.BaseAction;
import com.dartfar.base.model.Function;
import com.dartfar.base.model.Person;
import com.dartfar.base.service.PersonService;
import com.dartfar.base.vo.FunctionVo;
import com.dartfar.organize.vo.OrganizeTreeVo;
import com.dartfar.workflow.vo.WorkflowCurUserTasksVo;
import com.dartfar.workflow.vo.WorkflowViewBtnVo;

@Scope("prototype")
@Component("doAutAction")
public class DoAutAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private static Logger logger = Logger.getLogger(DoAutAction.class);
	
	/**
	 * 页面传参
	 */
	private String result;
	
	private String personId;
	private String departId;
	
	private String processId;
	private String idValue;
	private String dgName;
	
	
	/**
	 * 传递业务工作流参数
	 */
	public void addBusinessWorkflowParam(){
		
		logger.info("modelName: " + modelName);
		logger.info("modelIdName: " + modelIdName);
		logger.info("processId: " + processId);
		logger.info("idValue: " + idValue);
		
		
		WorkflowViewBtnVo wvbVo = new WorkflowViewBtnVo();
		wvbVo.setModelIdName(modelIdName);
		wvbVo.setModelName(modelName);
		wvbVo.setProcessId(processId);
		wvbVo.setIdValue(idValue);
		wvbVo.setDgName(dgName);
		session.put("bwf", wvbVo);
	}
	
	/**
	 * 获取业务流程参数
	 */
	public void loadBusinessWorkflowParam(){
		
		try {
			WorkflowViewBtnVo wvbVo = (WorkflowViewBtnVo) session.get("bwf");
			String jsonResult = JSONObject.fromObject(wvbVo).toString();
			response.getWriter().write(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取功能
	 */
	public void loadAutBtnData(){
		logger.info("doAutAction");
		try{
			List<FunctionVo> functionsVo = getAutFunctions();
			String jsonResult = JSONArray.fromObject(functionsVo).toString();
			logger.info("jsonResult: " + jsonResult);
		
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据浏览权限获取部门JSON
	 * @return OrganizeTreeVo的Json字符串
	 */
	public String doAutDepart(){
		List<OrganizeTreeVo> organizeTreeVos = new ArrayList<OrganizeTreeVo>();
		List<Function> functions = curAutView();
		for(Iterator<Function> f = functions.iterator();f.hasNext();){
			Function fun = f.next();
			
			if("viewAll".equals(fun.getFuncode())){
				OrganizeTreeVo organizeTreeVo = new OrganizeTreeVo();
				organizeTreeVo.setId("viewAll");
				organizeTreeVo.setText("全部");
				organizeTreeVos.add(organizeTreeVo);
				
				organizeTreeVo = new OrganizeTreeVo();
				organizeTreeVo.setId("viewDepart");
				organizeTreeVo.setText("本部门");
				organizeTreeVos.add(organizeTreeVo);
				
				organizeTreeVo = new OrganizeTreeVo();
				organizeTreeVo.setId("view");
				organizeTreeVo.setText("自己");
				organizeTreeVos.add(organizeTreeVo);
			}	
			
			if("viewDepart".equals(fun.getFuncode())){
				OrganizeTreeVo organizeTreeVo = new OrganizeTreeVo();
				organizeTreeVo.setId(fun.getCode());
				organizeTreeVo.setText("本部门");
				organizeTreeVos.add(organizeTreeVo);
				
				organizeTreeVo = new OrganizeTreeVo();
				organizeTreeVo.setId("view");
				organizeTreeVo.setText("自己");
				organizeTreeVos.add(organizeTreeVo);
			}
				
			if("view".equals(fun.getFuncode())){
				OrganizeTreeVo organizeTreeVo = new OrganizeTreeVo();
				organizeTreeVo.setId(fun.getCode());
				organizeTreeVo.setText("自己");
				organizeTreeVos.add(organizeTreeVo);
			}
			
			if("viewParentDepart".equals(fun.getFuncode())){
				OrganizeTreeVo organizeTreeVo = new OrganizeTreeVo();
				organizeTreeVo.setId(fun.getCode());
				organizeTreeVo.setText("上级部门");
				organizeTreeVos.add(organizeTreeVo);
			}	

		}
			
		String jsonResult = JSONArray.fromObject(organizeTreeVos).toString();
		try{
			response.getWriter().write(jsonResult);
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 根据编辑权限判断是否可以编辑
	 * @return result的Json字符串
	 */
	public String doAutEdit(){
		String flag = "false";
		List<Function> functions = curAutEdit();
		//Person person = personService.getPersonById(id);
			
		for(Iterator<Function> f = functions.iterator();f.hasNext();){
			Function fun = f.next();
			//编辑自己
			if(fun.getFuncode().equalsIgnoreCase("edit")){
				if(personId.equalsIgnoreCase(String.valueOf(curPerson().getPersonId()))){
					flag = "true";
					break;
				}
			}else
			//编辑本部门	
			if (fun.getFuncode().equalsIgnoreCase("editDepart")){
				if(departId.equalsIgnoreCase(String.valueOf(curDepart().getId()))){
					flag = "true";
					break;
				}
			}else
			//编辑全部	
			if(fun.getFuncode().equalsIgnoreCase("editAll")){
				flag = "true";
				break;
			}
			
		}
		result = flag;
		return SUCCESS;
	}
	
	/**
	 * 根据删除权限判断是否可以删除
	 * @return result的Json字符串
	 */
	public String doAutDel(){
		String flag = "false";
		List<Function> functions = curAutDel();
		logger.info("personId: " + personId);
		logger.info("departId: " + departId);
		try{
			
		
		for(Iterator<Function> f = functions.iterator();f.hasNext();){
			Function fun = f.next();
			//删除自己
			if(fun.getFuncode().equalsIgnoreCase("del")){
				if(personId.equalsIgnoreCase(String.valueOf(curPerson().getPersonId()))){
					flag = "true";
					break;
				}
			}else
			//删除本部门	
			if (fun.getFuncode().equalsIgnoreCase("delDepart")){
				if(departId.equalsIgnoreCase(String.valueOf(curDepart().getId()))){
					flag = "true";
					break;
				}
			}else
				//删除全部	
			if(fun.getFuncode().equalsIgnoreCase("delAll")){
					flag = "true";
					break;
			}	
		}
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		result = flag;
		return SUCCESS;
	}

	

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getPersonId() {
		return personId;
	}

	public void setPersonId(String personId) {
		this.personId = personId;
	}

	public String getDepartId() {
		return departId;
	}

	public void setDepartId(String departId) {
		this.departId = departId;
	}

	public String getProcessId() {
		return processId;
	}

	public void setProcessId(String processId) {
		this.processId = processId;
	}

	public String getIdValue() {
		return idValue;
	}

	public void setIdValue(String idValue) {
		this.idValue = idValue;
	}

	public String getDgName() {
		return dgName;
	}

	public void setDgName(String dgName) {
		this.dgName = dgName;
	}
	
	
	
	
}
